import { http } from "@/utils/http";
import { sysUrlApi } from "../utils";

export type addUserForm = {
  username: string;
  password: string;
  status: number;
  accountLv: number;
  orgId: string;
  nickname: string;
  email: string;
  cityCode: string;
  roleIds: string[];
};

// 添加用户
export const addUser = (data: addUserForm) => {
  return http.request("post", sysUrlApi("user/add"), { data });
};

export type updateUserForm = {
  userId: string;
  status: number;
  accountLv: number;
  orgId: string;
  nickname: string;
  email: string;
  cityCode: string;
};

// 更新用户信息
export const updateUser = (data: updateUserForm) => {
  return http.request("put", sysUrlApi("user/update"), { data });
};

export type updatePasswordForm = {
  oldPassword: string;
  newPassword: string;
};

// 用户更新自己的密码
export const updatePasswordBySelf = (data: updateUserForm) => {
  return http.request("put", sysUrlApi("user/pwd"), { data });
};

// 重置用户的密码
export const resetUserPassword = (id: string) => {
  return http.request("put", sysUrlApi("user/pwd/" + id));
};

// 删除用户
export const deleteUser = (id: string) => {
  return http.request("delete", sysUrlApi("user/delete/" + id));
};

export type searchUserForm = {
  username: string;
  orgId: string;
  pageNum: number;
  pageSize: number;
};
export type Result = {
  list: Array<any>;
  total?: number;
};
// 获取用户分页信息
export const getUserPage = (params: searchUserForm) => {
  return http.request<Result>("get", sysUrlApi("user/page"), { params });
};

export type userBindOrg = {
  userId: string;
  orgIds: string[];
};

// 获取已经绑定的部门id列表
export const getBoundOrgIds = (uid: string) => {
  return http.request<string[]>("get", sysUrlApi("user/boundOrg/" + uid));
};

// 分配部门管理权限
export const bindOrg = (data: userBindOrg) => {
  return http.request("put", sysUrlApi("user/bindOrg"), { data });
};

export type userBindRole = {
  userId: string;
  roleIds: string[];
};
// 获取已经绑定的角色id列表
export const getBoundRoleIds = (uid: string) => {
  return http.request<string[]>("get", sysUrlApi("user/boundRole/" + uid));
};
// 分配角色
export const bindRole = (data: userBindRole) => {
  return http.request("put", sysUrlApi("user/bindRole"), { data });
};

// 获取用户列表
export const getUserList = () => {
  return http.request<Array<any>>("get", sysUrlApi("user/list"));
};
